package com.cow.framework.core.config.dict;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.core.exception.BusinessException;
import com.cow.sram.comm.config.constant.MessageConstants;
@Service
public class DictService {
	@Autowired
	DictDAO dictDAO;
	
	/**根据ID查询字典*/
	public Dict getDictById(Integer id){
		return dictDAO.findOne(id);
	}
	
	/**根据类型查询字典*/
	public List<Dict> findByType(String type){
		return dictDAO.findByTypeOrderByValue(type);
	}
	
	/**保存字典*/
	public Dict saveDict(Dict dict){
		if(isExist(dict.getType(),dict.getValue())){
			throw new BusinessException("H1000",MessageConstants.H1000);
		}
		return dictDAO.save(dict);
	}
	
	/**删除字典*/
	public void delDict(Integer id){
		Dict dict =dictDAO.findOne(id);
		dictDAO.delete(dict);
	}
	
	/**查询字典类型列表*/
	public List<Dict> findDictTypeList(){
		return dictDAO.findDictTypeList();
	}
	
	/**判断字典是否已存在*/
	public boolean isExist(String type,String value){
		List<Dict> list = dictDAO.findByTypeAndValue(type,value);		
		return list != null && list.size() > 0;
	}
}
